package com.raising.framework.service;

import com.raising.framework.dao.ICrudDao;
import com.raising.framework.entity.BaseEntity;
import com.raising.framework.entity.ResultCode;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service基类
 * @author gaoy
 */
@Transactional(rollbackFor = Exception.class)
public abstract class CrudService<D extends ICrudDao<T>, T extends BaseEntity>
        extends BaseService {

    /**
     * 持久层对象
     */
    @Autowired
    protected D dao;

    /************************************* 查询 ******************************************/

    /**
     * 获取单条数据
     * @author GaoYuan
     * @param id
     * @datetime 2017/12/6 上午9:37
     * @return
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public T get(String id) throws Exception{
        return dao.get(id);
    }

    /**
     * 根据参数查询单个数据
     * @author GaoYuan
     * @date 2018/11/21 下午5:28
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public T getByParam(T entity) throws Exception{
        return dao.getByParam(entity);
    }

    /**
     * 查询列表数据
     * @author GaoYuan
     * @param entity
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<T> getList(T entity) throws Exception{
        return dao.getList(entity);
    }
    /**
     * 查询分页
     * @author GaoYuan
     * @param page
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public Page<T> getPage(Page<T> page) throws Exception{
        page.setResults(dao.getPage(page));
        return page;
    }


    /************************************* 事务类操作 ******************************************/


    /**
     * 保存数据（插入或更新）
     * @author GaoYuan
     * @param entity
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVo save(T entity) throws Exception {
        if (entity.getIsNewId()) {
            entity.preInsert(null);
            entity.setId(this.nextId());
            dao.insert(entity);
        } else {
            entity.preUpdate(null);
            dao.update(entity);
        }
        return ResultVo.success().data(entity.getId()).desc("操作成功");
    }

    /**
     * 插入
     * @author GaoYuan
     * @param entity
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVo insert(T entity) throws Exception{
        entity.preInsert(null);
        if(entity.getId() == null || "".equals(entity.getId())){
            entity.setId(this.nextId());
        }
        dao.insert(entity);
        return ResultVo.success().data(entity.getId()).desc("操作成功");
    }

    /**
     * 更新
     * @author GaoYuan
     * @param entity
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVo update(T entity) throws Exception{
        entity.preUpdate(null);
        dao.update(entity);
        return ResultVo.success().data(entity.getId()).desc("操作成功");
    }

    /**
     * 删除数据
     * @author GaoYuan
     * @param id 主键
     * @datetime 2017/12/6 上午9:37
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVo delete(String id) throws Exception{
        int result = dao.delete(id);
        if(result>0){
            return ResultVo.success().desc("成功删除");
        }else{
            return ResultVo.failure(ResultCode.RECORD_EMPTY).desc("没有需要操作的数据");
        }
    }

}
